<?php
require_once '../common/Page.php';
require_once '../common/GeoMgr.php';
require_once '../common/main.php';

$lat = $_REQUEST["lat"];
$lng = $_REQUEST["lng"];
$address = $_REQUEST["address"];
$action = $_REQUEST["action"];

$params = array();
$params["lat"] = $lat;
$params["lng"] = $lng;

Page::makeHeader("Géolocalisation", 3, $params);

if($action == "SAVE")
{
	$nb = GeoMgr::savePosition($dbMgr, $lat, $lng, $address);
	if($nb > 0)
		echo "<script type=\"text/javascript\">alert('Position actuelle sauvegardée !');</script>";
}
elseif($action == "LOAD_ALL")
{
	$allPositions = GeoMgr::loadAllPositions($dbMgr);
        $liste_positions = GeoMgr::showPositionsLeftList($allPositions);
	$nb = count($allPositions);
	if($nb > 0)
	{
		echo "<script type=\"text/javascript\">alert('Positions chargées !');</script>";
		$positions_loaded = 1;
	}
        
}
?>

<div id="div_geoloc">
    <div id="menu_geoloc">
        <div id="geocodage">
            <form id="from_adresse" action="geolocalisation.php" method="get" >
                <div id="div_form">
		    <label for="geocode_input" >Entrer votre adresse : </label>
                    <input id="geocode_input" type="text" <?php echo "value=\"$address\"" ?> />
		    <input id="geocode_lat" type="hidden" value=""/>
		    <input id="geocode_lng" type="hidden" value=""/>
		    <input id="geocode_button" type="submit" value="Géolocaliser par adresse postale" onclick="geoloc_adresse();" />
                </div>
            </form>
        </div>
        <div id="localisation_ip">
            <input id="geoloc_button" type="submit" value="Géolocaliser par adresse IP" onclick="geoloc_ip();" />
        </div>
    </div>

    <div id="liste_positions">
	<?php
		
			echo "<div id=\"current_position\">";
			echo "<h1>Position actuelle</h1>";
			if(isset($lat) && isset($lng))
                            echo "<textarea title='date_position' cols='1' rows='3' class=\"current_position_adress\" readonly='readonly' >$address</textarea>";
                        else
                            echo "<p> Aucune position actuelle </p>";
			//echo "<input class=\"current_position_go\" type=\"submit\" value=\"Afficher\" />";
			echo"</div>";
		
		if($positions_loaded == 1)
                {
			echo $liste_positions;
                }
	?>
    </div>
    <div id="div_map"></div>
    <div id="menu_autre">
        <input id="save_position_button" type="submit" value="Sauvegarder position" onclick="savePosition();" />
        <input id="load_positions_button" type="submit" value="Charger positions" onclick="getAllPositions();" />
    </div>
</div>

<?php
Page::makeFooter();
?>
